package com.androidxmpp.fragment;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.androidxmpp.ChatActivity;
import com.androidxmpp.R;
import com.androidxmpp.adapter.RecentChatAdapter;
import com.androidxmpp.db.ChatProvider;
import com.androidxmpp.db.ChatProvider.ChatConstants;
import com.androidxmpp.ui.BaseSwipeListViewListener;
import com.androidxmpp.ui.SwipeListView;
import com.androidxmpp.util.LogUtil;
import com.androidxmpp.util.XMPPHelper;

public class RecentChatFragment extends Fragment implements OnClickListener {

	private View view;
	private FragmentCallBack mFragmentCallBack;
	
	private TextView ivTitleName;
	
	private SwipeListView listView;
	private RecentChatAdapter chatAdapter;
	private Handler mainHandler = new Handler();
	private ContentObserver mChatObserver = new ChatObserver();
	private ContentResolver mContentResolver;
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mFragmentCallBack = (FragmentCallBack) activity;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContentResolver = getActivity().getContentResolver();
		chatAdapter = new RecentChatAdapter(getActivity());
	}

	@Override
	public void onResume() {
		super.onResume();
		chatAdapter.requery();
		mContentResolver.registerContentObserver(ChatProvider.CONTENT_URI,
				true, mChatObserver);
	}

	@Override
	public void onPause() {
		super.onPause();
		mContentResolver.unregisterContentObserver(mChatObserver);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.frame_recentchat, null);
		
		ivTitleName = (TextView) view.findViewById(R.id.ivTitleName);
		ivTitleName.setVisibility(View.VISIBLE);
		ivTitleName.setText("最近会话");
		
		listView = (SwipeListView) view.findViewById(R.id.recent_listview);
		listView.setEmptyView(view.findViewById(R.id.empty));
		listView.setAdapter(chatAdapter);
		listView.setSwipeListViewListener(mSwipeListViewListener);
		
		return view;
	}
	
	BaseSwipeListViewListener mSwipeListViewListener = new BaseSwipeListViewListener() {
		@Override
		public void onClickFrontView(int position) {
			Cursor clickCursor = chatAdapter.getCursor();
			clickCursor.moveToPosition(position);
			String jid = clickCursor.getString(clickCursor
					.getColumnIndex(ChatConstants.JID));
			Uri userNameUri = Uri.parse(jid);
			Intent toChatIntent = new Intent(getActivity(), ChatActivity.class);
			toChatIntent.setData(userNameUri);
			toChatIntent.putExtra(ChatActivity.INTENT_EXTRA_USERNAME,
					XMPPHelper.splitJidAndServer(jid));
			startActivity(toChatIntent);
		}

		@Override
		public void onClickBackView(int position) {
			listView.closeOpenedItems();// 关闭打开的项
		}
	};

	
	public void updateRoster() {
		chatAdapter.requery();
	}

	private class ChatObserver extends ContentObserver {
		public ChatObserver() {
			super(mainHandler);
		}

		public void onChange(boolean selfChange) {
			updateRoster();
			LogUtil.i("liweiping", "selfChange" + selfChange);
		}
	}
	
	@Override
	public void onClick(View arg0) {
		
	}

	
}
